<template>
  <div class="scorePage">
    <nav class="navbar navbar-expand-lg navbar-light bg-light">
      <div class="container-fluid header">
        <h1>学员成绩</h1>
        <p>点击表格修改或录入</p>
      </div>
    </nav>

    <!-- table-striped隔行换色 -->
    <table class="table table-striped">
      <colgroup>
        <col width="14%" />
        <col width="16%" />
        <col width="14%" />
        <col width="14%" />
        <col width="14%" />
        <col width="14%" />
        <col width="14%" />
      </colgroup>
      <thead>
        <tr>
          <th scope="col">学员ID</th>
          <th scope="col">姓名</th>
          <th scope="col">第1次成绩</th>
          <th scope="col">第2次成绩</th>
          <th scope="col">第3次成绩</th>
          <th scope="col">第4次成绩</th>
          <th scope="col">第5次成绩</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="(item, key) in list" :key="key">
          <th scope="row">{{ key }}</th>
          <td>{{ item.name }}</td>
          <!-- <td class="score">{{ item.score[0] }}</td>
          <td class="score">{{ item.score[1] }}</td>
          <td class="score">{{ item.score[2] }}</td>
          <td class="score">{{ item.score[3] ? item.score[3] : "null" }}</td>
          <td class="score">{{ item.score[4] ? item.score[4] : "null" }}</td> -->
          <td
            class="score"
            v-for="(everyScore, index) in item.score"
            :key="index"
            @dblclick="ondblclick(key, index, everyScore)"
          >
            {{ everyScore ? everyScore : "null" }}
            <input
              type="text"
              v-if="selectIndex === index && selectId === key"
              v-model="lastScore"
              @keyup.enter="changeScore(key, index)"
              @blur="changeScore(key, index)"
              ref="input"
            />
            <!-- ref 获取 dom 元素 用于触发焦点事件 -->
          </td>
        </tr>
      </tbody>
    </table>
  </div>
</template>

<script>
import { getScoresAPI, ChangeScoreAPI } from "@/api/score";

export default {
  data() {
    return {
      list: {},
      lastScore: "",
      selectIndex: "",
      selectId: "",
      time: Date.now(),
    };
  },
  async created() {
    // const res = await getScoresAPI();
    // console.log(res.data.data);
    // this.list = res.data.data;
    // console.log(this.list);
    this.getVueScore();
  },
  methods: {
    // 获取成绩
    async getVueScore() {
      const res = await getScoresAPI();
      this.list = res.data.data;
    },
    ondblclick(key, index, score) {
      this.selectIndex = index;
      this.selectId = key;
      this.lastScore = score;
      // console.log(this.list[key].score[index]);
      // this.$refs.input.focus(); // 异步问题，vue 底层操作dom 没有那么快
      // 需要 nextTick 钩子 实现
      this.$nextTick(() => {
        // console.log(this.$refs.input); // 放在 v-for 中，打印出来的是数组 [input]
        this.$refs.input[0].focus();
      });
    },
    async changeScore(key, index) {
      if (Date.now() - this.time > 400) {
        this.time = Date.now();
        // 通过 “节流” 来解决 回车事件 和 失焦事件 触发两次 changeScore 函数的问题
        this.list[key].score[index] = +this.lastScore;
        const res = await ChangeScoreAPI(
          this.selectId,
          this.selectIndex + 1,
          this.lastScore
        );
        this.lastScore = "";
        console.log(this.lastScore);
        // console.log(res);
        this.selectIndex = -1;
      }
    },
  },
};
</script>

<style scoped>
.scorePage {
  background-color: #f1f1f1;
  color: #6d767e;
  padding: 15px;
  box-sizing: border-box;
  font-size: 14px;
}

* {
  margin: 0;
  padding: 0;
}

.header {
  height: 36px !important;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.header h1 {
  font-size: 18px;
  margin-bottom: 0 !important;
}

/* 表格 */
.table {
  margin: 10px auto;
  width: 100%;
  border-left: solid 1px #ccc;
  border-top: solid 1px #ccc;
  border-spacing: 0;
  border-radius: 4px;
  background-color: #fff;
  font-size: 12px;
}

.table th,
.table td {
  border-right: solid 1px #ccc;
  border-bottom: solid 1px #ccc;
  padding: 8px 0;
  text-align: center;
}

.table tr th:last-child {
  border-radius: 0 4px 0 0;
}

.table tbody tr:last-child td:first-child {
  border-radius: 0 0 0 4px;
}

.table tbody tr:last-child td:last-child {
  border-radius: 0 0 4px 0;
}
.table tr {
  line-height: 32px;
}
/* .btn-primary {
  background-color: #747ced !important;
  border-color: #747ced !important;
} */
.table .btn-sm {
  zoom: 0.8 !important;
}
.float {
  float: right;
  margin-bottom: 10px;
}
.flex {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
}
@media (min-width: 576px) {
  .modal-dialog {
    width: 385px !important;
  }
}
.flex {
  display: flex;
}
.col-auto {
  height: 30px !important;
  margin-top: 0 !important;
}
.col-form-label {
  font-size: 14px !important;
}

tbody td {
  position: relative;
}
tbody input {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  border: none;
  /* background-color: #fff; */
  background-color: yellow;
  outline-color: blue;
  text-align: center;
}
</style>
